import typing
from typing import Optional

import PySide6.QtCore
from PySide6.QtGui import QIcon
import PySide6.QtWidgets
from PySide6.QtCore import Qt

from ..LittleWidgetBase import LittleWidgetBase
from .Ui_LWCombox import Ui_LWCombox

class LWCombox(LittleWidgetBase):
    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Widget) -> None:
        ################################################
        super().__init__(parent=parent, f=f)
        self._ui = Ui_LWCombox()
        self._ui.setupUi(self)
        self._propertys.update({
            'currentText': '',
            'options': list(),
        })
        self._ui.comboBox.currentTextChanged.connect(self._SlotComboxTextChanged)
        self._currentText = ''


    def UpdateProperty(self, key: 'str', value: typing.Any) -> None:
        super().UpdateProperty(key, value)
        if (key == 'currentText'):
            if (not isinstance(value, str)):
                return
            self._ui.comboBox.setCurrentText(value)
            self._UpdatePropertyValue({'currentText': value})
            self._currentText = value
        elif (key == 'options'):
            if (not isinstance(value, list)):
                return
            # current_text_ = self._ui.comboBox.currentText()
            self._ui.comboBox.currentTextChanged.disconnect()
            self._ui.comboBox.clear()
            self._ui.comboBox.addItems(value)
            self._ui.comboBox.setCurrentText(self._currentText)
            self._ui.comboBox.currentTextChanged.connect(self._SlotComboxTextChanged)
            self._SlotComboxTextChanged(self._ui.comboBox.currentText())
            
    def _SlotComboxTextChanged(self, text: 'str') -> None:
        '''
        function:
            当 Combox 中的项目文字变化的时候做什么
        '''
        self._UpdatePropertyValue({'currentText': text})
        self._currentText = text
        self.signalEvent.emit(self.EventComboxChanged, self)